﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using PPI.Entities.Distribution;
using PPI.Entities.Interface;
using PPI.Mobile;
using VMind.Global;
using PPIDataAccess.Distribution;

namespace PPI.ServiceLogic.Distribution
{
    public class LPhase : IMobileTable
    {
        public static DataSet SelectAllIdName(string connectionKey)
        {
            return DPhase.SelectAll(connectionKey);
        }

        public static Int32 Insert(EPhase phase, Int32 poleId, string connectionString)
        {
            return DPhase.Insert(phase, poleId, connectionString);
        }

        public static void Update(List<EPhase> phases, Int32 poleId, string connectionString)
        {
            DPhase.Update(phases, poleId, connectionString);
        }

        public List<EMobileQuery> CreateInsertMobileQuery(string connectionKey)
        {
            DataTable phases = DPhase.SelectAll(connectionKey).Tables[0];

            List<EMobileQuery> listQueries = new List<EMobileQuery>();
            listQueries.Add(new EMobileQuery(string.Format("DELETE tb_Lookups WHERE LookupTypeFk = '{0}'", "Phase")));

            EMobileQuery insertQuery = new EMobileQuery("INSERT INTO tb_Lookups (LookupValue, LookupText, LookupTypeFk) VALUES (@id, @Name, @type)");

            foreach (DataRow phase in phases.Rows)
            {
                List<SqlParameter> parameters = new List<SqlParameter>();

                parameters.Add(DBHelper.CreateIntParameter("@id", ParameterDirection.Input, false, Convert.ToInt32(phase["PhaseId"])));
                parameters.Add(DBHelper.CreateNVarCharParameter("@Name", 100, ParameterDirection.Input, false, phase["Name"]));
                parameters.Add(DBHelper.CreateNVarCharParameter("@type",100, ParameterDirection.Input, false, "Phase"));

                insertQuery.Parameters.Add(parameters);
            }

            listQueries.Add(insertQuery);
            
            return listQueries;
        }

    }
}
